package com.zhonghui.modlules.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhonghui.common.eshopConstant.ShopConstants;
import com.zhonghui.common.eshopEnum.ProductEnum;
import com.zhonghui.common.eshopService.StoreCanvasService;
import com.zhonghui.common.eshopService.StoreCombinationService;
import com.zhonghui.common.eshopService.StoreSeckillService;
import com.zhonghui.common.eshopUtil.ApiResult;
import com.zhonghui.common.eshopVo.StoreCombinationQueryVo;
import com.zhonghui.common.eshopVo.StoreProductQueryVo;
import com.zhonghui.modlules.product.eshopService.StoreProductService;
import com.zhonghui.modlules.product.vo.IndexVo;
import com.zhonghui.modlules.shop.eshopDomain.StoreCanvas;
import com.zhonghui.modlules.shop.eshopService.SystemGroupDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(value = "首页模块",tags = "首页模块")
public class IndexController {

    private final StoreProductService storeProductService;
    private final StoreSeckillService storeSeckillService;
    private final StoreCanvasService storeCanvasService;
    private final StoreCombinationService storeCombinationService;
    private final SystemGroupDataService systemGroupDataService;

    @ApiOperation(value = "读取画布数据",notes = "读取画布数据")
    @GetMapping("getCanvas")
    public ResponseEntity<StoreCanvas> getCanvas(StoreCanvas storeCanvas){
        StoreCanvas canvas = storeCanvasService.getOne(new LambdaQueryWrapper<StoreCanvas>()
                .eq(StoreCanvas::getTerminal, storeCanvas.getTerminal())
                .orderByDesc(StoreCanvas::getCanvasId).last("limit 1"));
        return new ResponseEntity<>(canvas, HttpStatus.OK);
    }

    @ApiOperation(value = "首页数据",notes = "首页数据")
    @GetMapping("/index")
    public ApiResult<IndexVo> index(){
        IndexVo index = IndexVo.builder()
                .banner(systemGroupDataService.getDatas(ShopConstants.YSHOP_HOME_BANNER))
                .menus(systemGroupDataService.getDatas(ShopConstants.YSHOP_HOME_MENUS))
                .roll(systemGroupDataService.getDatas(ShopConstants.YSHOP_HOME_ROLL_NEWS))

                .bastList(storeProductService.getList(1, 6, ProductEnum.TYPE_1.getValue()))
                .likeInfo(storeProductService.getList(1, 8, ProductEnum.TYPE_2.getValue()))
                .firstList(storeProductService.getList(1, 6, ProductEnum.TYPE_3.getValue()))
                .benefit(storeProductService.getList(1, 10, ProductEnum.TYPE_4.getValue()))

                .combinationList(storeCombinationService.getList(1, 8).getStoreCombinationQueryVos())
                .seckillList(storeSeckillService.getList(1, 4))
                .build();
        return ApiResult.ok(index);
    }

    @ApiOperation(value = "精品推荐",notes = "精品推荐")
    @GetMapping("/bastList")
    public ApiResult<List<StoreProductQueryVo>> bastList(){
        return ApiResult.ok(storeProductService.getList(1,6,ProductEnum.TYPE_1.getValue()));
    }

    @ApiOperation(value = "猜你喜欢",notes ="猜你喜欢" )
    @GetMapping("/benefit")
    public ApiResult<List<StoreProductQueryVo>> benefit(){
        return ApiResult.ok(storeProductService.getList(1,10,ProductEnum.TYPE_4.getValue()));
    }

    @ApiOperation(value = "拼团",notes = "拼团")
    @GetMapping("/combination")
    public ApiResult<List<StoreCombinationQueryVo>> Combination(){
        return ApiResult.ok(storeCombinationService.getList(1,8).getStoreCombinationQueryVos());
    }

    @ApiOperation(value = "首页菜单",notes = "首页菜单")
    @GetMapping("/menus")
    public ApiResult<List<JSONObject>> menus(){
        return ApiResult.ok(systemGroupDataService.getDatas(ShopConstants.YSHOP_HOME_MENUS));
    }

    @ApiOperation(value = "轮播图",notes = "轮播图")
    @GetMapping("/banner")
    public ApiResult<List<JSONObject>> banner(){
        return ApiResult.ok(systemGroupDataService.getDatas(ShopConstants.YSHOP_HOME_BANNER));
    }
}
